package com.superteam.zzl.activity.MineActivity;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.superteam.zzl.activity.BaseActivity;
import com.superteam.zzl.activity.QQLoginActivity;
import com.superteam.zzl.activity.R;
import com.superteam.zzl.activity.databinding.ActivityLoginBinding;

import java.util.HashMap;
import java.util.Objects;
import java.util.Set;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;

public class LoginActivity extends BaseActivity implements PlatformActionListener{

    private ActivityLoginBinding loginBinding;
    private EditText et_name,et_pwd;
    private Handler handler;

    @Override
    public void widgetClick(View v) {

        switch (v.getId()){
            case R.id.iv_close://关闭当前页
                setResult(300,getIntent());
                finish();
                break;
            case R.id.tv_login_login://登录选项
                loginBinding.rlSelect.setVisibility(View.GONE);
                loginBinding.tvOther.setVisibility(View.GONE);
                loginBinding.lyOther.setVisibility(View.GONE);
                loginBinding.lyLogin.setVisibility(View.VISIBLE);
                loginBinding.ivBack.setVisibility(View.VISIBLE);
                break;
            case R.id.tv_regist_login://注册选项
                startActivityForResult(RegistActivity.class,null,300);
                break;
            case R.id.iv_qq://qq登录
                //showToast("qq登录");
                qqLogininit();
                qqLogin();
                break;
            case R.id.iv_weibo://微博登录
                showToast("微博登录");
                break;
            case R.id.tv_real_login://输入后登录按钮
                break;
            case R.id.iv_back://回退键
                loginBinding.rlSelect.setVisibility(View.VISIBLE);
                loginBinding.tvOther.setVisibility(View.VISIBLE);
                loginBinding.lyOther.setVisibility(View.VISIBLE);
                loginBinding.lyLogin.setVisibility(View.GONE);
                loginBinding.ivBack.setVisibility(View.GONE);
                break;
        }
    }

    @Override
    public ViewDataBinding initDataBinding() {
        loginBinding = DataBindingUtil.setContentView(this,R.layout.activity_login);
        et_name = loginBinding.etName;
        et_pwd = loginBinding.etPwd;
        return loginBinding;
    }

    @Override
    public void initParms(Bundle parms) {

    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public void initView(View view) {

    }

    @Override
    public void setListener() {

    }

    @Override
    public void doBusiness(Context mContext) {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
//        if(requestCode ==556 && resultCode == 258){
//            Log.e("data","Login");
//            setResult(444,data);
//            finish();
//        }
    }

    private void qqLogininit() {
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                //    Set<String> keys=()msg
                HashMap<String,Objects> map = (HashMap<String, Objects>) msg.obj;
                //    Set<String> keys=map.keySet();
                //tv.setText(String.valueOf(map.get("nickname")));
                //final BitmapPool bitmapPool = Glide.get(getApplicationContext()).getBitmapPool();
//                Glide.with(getApplicationContext()).load(map.get("figureurl_qq_2"))
//                        .bitmapTransform(new CropCircleTransformation(bitmapPool)).into(iv);
                Log.e("handleMessage: ", String.valueOf(map.get("nickname")));
                Intent intent = getIntent();
                intent.putExtra("name",map.get("nickname").toString());
                intent.putExtra("pic",map.get("figureurl_qq_2").toString());
                setResult(258,intent);
                finish();


            }
        };
    }

    private void qqLogin()
    {
        //获取对应的平台对象
        Platform pf= ShareSDK.getPlatform(QQ.NAME);
        pf.authorize();//调用授权
        //设置登陆结果的回调接口
        pf.setPlatformActionListener(this);
        pf.showUser(null);//显示用户信息
    }

    @Override
    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {

        Set<String> keys=hashMap.keySet();
        for (String key:keys)
        {
            Log.e("TS", "onComplete: 键="+key+",值="+hashMap.get(key));
        }
        Message msg=handler.obtainMessage(0x110);
        msg.obj=hashMap;
        handler.sendMessage(msg);
    }

    @Override
    public void onError(Platform platform, int i, Throwable throwable) {
        Log.e("data","error");
    }

    @Override
    public void onCancel(Platform platform, int i) {
        Log.e("data","onCancel");
    }
}
